﻿using EONFULLSIN2.Common;
using EONFULLSIN2.Models.Dto.User;
using EONFULLSIN2.Models.Entity.User;
using EONFULLSIN2.Services.DB;
using EONFULLSIN2.ViewModelBase;
using Prism.Commands;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EONFULLSIN2.ViewModel
{
    public class LoginViewModel : DialogViewModel
    {
        private IDbOperate<IFreeSqlMainContext> _mainDbOperate;
        private IDbOperate<IFreeSqlHisContext> _hisDbOperate;

        public LoginViewModel(IDbOperate<IFreeSqlMainContext> mainDbOperate, IDbOperate<IFreeSqlHisContext> hisDbOperate)
        {
            this._mainDbOperate = mainDbOperate;
            this._hisDbOperate = hisDbOperate;

            this.LoginName = "SuperAdmin";
            this.Password = "root123456";
        }

        #region Props

        public string GrowlToken => nameof(LoginViewModel);

        private string _loginName;
        // <summary>
        /// 登录名
        /// </summary>
        public string LoginName
        {
            get { return _loginName; }
            set { _loginName = value; RaisePropertyChanged(); }
        }

        private string _password;
        /// <summary>
        /// 密码
        /// </summary>
        public string Password
        {
            get { return _password; }
            set { _password = value; RaisePropertyChanged(); }
        }

        private bool _isOpen;

        public bool IsOpen
        {
            get { return _isOpen; }
            set { _isOpen = value; RaisePropertyChanged(); }
        }

        #endregion

        #region Commands
        /// <summary>
        /// 登录命令
        /// </summary>
        public DelegateCommand LoginCommand => new DelegateCommand(() =>
        {
            if (!IsVerify())
                return;

            var user = _mainDbOperate.GetModel<User>(p => p.Account == LoginName && p.PassWord == Password);
            if (user == null)
            {
                PrismIocInfo.MessageService.GrowError($"登录失败！{Environment.NewLine}用户名不存在或者密码错误", GrowlToken);
                return;
            }
            if (user.Status == false)
            {
                PrismIocInfo.MessageService.GrowError($"登录失败！{Environment.NewLine}该用户登录身份已失效,请联系系统管理员！", GrowlToken);
                return;
            }
            AppInfo.CurrentUser = user;
            this.OnDialogClosed(ButtonResult.OK);
        });
        #endregion

        #region Methods
        //验证
        private bool IsVerify()
        {
            if (string.IsNullOrEmpty(LoginName))
            {
                PrismIocInfo.MessageService.GrowWarning("登录名不能为空！", GrowlToken);
                return default;
            }
            if (string.IsNullOrEmpty(Password))
            {
                PrismIocInfo.MessageService.GrowWarning("密码不能为空！", GrowlToken);
                return default;
            }
            return true;
        }
        #endregion
    }
}
